package com.bestdreamer.thread_safe.singleton_enum;

import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;

public class MyObjectEnum {

    public enum MyEnumSingleton{
        connectionFactory;
        private Connection connection;
        private MyEnumSingleton(){
            try{
                System.out.println("创建 MyObject 对象");
                String url = "jdbc:sqlserver://localhost:3306;databaseName=mango";
                String username = "root";
                String password = "Dasiyebushuo123!";
                String driverName = "com.microsoft.sqlserver.jdbc.SQLServerDriver";
                Class.forName(driverName);
                connection = DriverManager.getConnection(url,username,password);
            }catch (ClassNotFoundException e){
                e.printStackTrace();
            }catch (SQLException e){
                e.printStackTrace();
            }
        }
        public Connection getConnection(){
            return connection;
        }
    }

    public static Connection getConnection(){
        return MyEnumSingleton.connectionFactory.getConnection();
    }
}
